<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UsersController extends Controller {

    public function index() {//会员列表
        $data = \DB::table("user")->get();
        return view("users.user_list")->with("data", $data);
    }

    public function ExitStatus(Request $request) {//修改会员是否验证
        $status = $request->status;
        $id = $request->id;
        if (\DB::update("update user set status=" . $status . " where id=" . $id)) {
            echo 1;
        }
    }

    public function create() {//会员添加页面
        return view("users.user_add");
    }

    public function store(Request $request) {//会员添加处理
        //获取数据
        $str = $_POST['str'];
        //将表单序列化的值转换成数组
        parse_str($str, $arr);
        $rule = [
            'name' => 'required|unique:user|between:6,12',
            'pass' => 'required|between:6,12',
        ];
        $message = [
            'name.required' => "请输入用户名",
            'name.unique' => "用户名已经存在",
            'name.between' => "用户名长度应在6-12个字符之间",
            'pass.required' => "请输入密码", 
            'pass.between' => "密码长度应在6-12个字符之间",
        ];
        $validator = \Validator::make($arr, $rule, $message);
        if ($validator->passes()) {
            $arr['time'] = time(); 
            if (\DB::table("user")->insert($arr)) {
                echo 1;
            }
          } else {
            return $validator->getMessageBag()->getMessages();
          }
    }
    public function show($id){//会员编辑修改页面
        $data=\DB::table("user")->where("id",$id)->first();
        return view("users.user_edit")->with("data",$data);
    }
    public function update(Request $request){//会员编辑修改处理
         //获取数据 
        $str = $_POST['str'];
        //将表单序列化的值转换成数组
        parse_str($str, $arr);
        if($arr['pass']==$arr['repass']){
          unset($arr['repass']);  
        }
        if(\DB::table("user")->where("id",$arr['id'])->update($arr)){
            echo 1;
        }
    }
    
    public function account($id){//会员资金管理页面
        $data=\DB::table("user")->where("id",$id)->first();
        return view('users.account')->with("data",$data);
    }
    
    public function cashEdit(Request $request){
         //获取数据 
        $str = $_POST['str'];
        //将表单序列化的值转换成数组
        parse_str($str, $arr); 
        $info=[];
        $info['uid']=$arr['id'];
        $data=\DB::table("user")->where('id',$arr['id'])->first(); 
        if($arr['operate']=="reduce"){
            $data->money==0?$money=0:$money=$data->money-$arr['money'];
            $money<=0?$money=0:$money=$money;
            $info['record']="-".$arr['money'].".00";
        }else{
            $money=$arr['money']+$data->money;
            $info['record']="+".$arr['money'].".00";
        }
        $arr['money']=$money;
        unset($arr['operate']); 
        $info['record_time']= time();
        if(\DB::table("user")->where("id",$arr['id'])->update($arr)){
            if(\DB::table("recharge")->insert($info)){
                echo "1";
            }
        }
    }
    public function comments(){
        $data=\DB::table("comment")->get(); 
        return view("users.user_message")->with("data",$data);
    }

//    |        | POST      | admin/users                                | users.store            | App\Http\Controllers\admin\UsersController@store      | web,admin.login |
//|        | GET|HEAD  | admin/users                                | users.index            | App\Http\Controllers\admin\UsersController@index      | web,admin.login |
//|        | GET|HEAD  | admin/users/create                         | users.create           | App\Http\Controllers\admin\UsersController@create     | web,admin.login |
//|        | PUT|PATCH | admin/users/{user}                         | users.update           | App\Http\Controllers\admin\UsersController@update     | web,admin.login |
//|        | DELETE    | admin/users/{user}                         | users.destroy          | App\Http\Controllers\admin\UsersController@destroy    | web,admin.login |
//|        | GET|HEAD  | admin/users/{user}                         | users.show             | App\Http\Controllers\admin\UsersController@show       | web,admin.login |
//|        | GET|HEAD  | admin/users/{user}/edit                    | users.edit             | App\Http\Controllers\admin\UsersController@edit       | web,admin.login |
}
